﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNAchievement.Achievements
{
    /// <summary>
    /// Represents a achievement that is unlocked when all flags are set.
    /// Use for achievements like "find one of each cards" or "visit each level
    /// one time". Don't forget to call RegisterFlags!
    /// Extra tip: Derive a enum from int to make the Set/ClearFlag call more clear!
    /// </summary>
    public class FlagsAchievement : Achievement
    {
        Dictionary<int, bool> flags = new Dictionary<int, bool>();

        /// <summary>
        /// Creates a locked achievements with the given internal name, using the given texture names.
        /// If no image is needed pass null for the name.
        /// </summary>
        public FlagsAchievement(string internalName, string lockedTextureName, string unlockedTextureName):
            base(internalName, lockedTextureName, unlockedTextureName)
        {
        }
        
        /// <summary>
        /// For internal use only.
        /// </summary>
        public FlagsAchievement()
        {
        }

        /// <summary>
        /// Sets the flag with the given ID.
        /// </summary>
        public void SetFlag(int id)
        {
            flags[id] = true;

            if (flags.All(x => x.Value == true))
                Unlock();
        }

        /// <summary>
        /// Clears the flag with the given ID.
        /// </summary>
        /// <param name="id"></param>
        public void ClearFlag(int id)
        {
            flags[id] = false;
        }

        /// <summary>
        /// Registers flags with the given IDs.
        /// </summary>
        public void RegisterFlags(params int[] flagsToReg)
        {
            foreach (var i in flagsToReg)
            {
                flags.Add(i, false);
            }
        }

        /// <summary>
        /// Registers flags with IDs from start to end (both incl.)
        /// </summary>
        public void RegisterFlagsFromTo(int start, int end)
        {
            for (int i = start; i <= end; i++)
            {
                flags.Add(i, false);
            }
        }
    }
}
